package com.sunnada.nginx.conf;


import com.sunnada.nginx.common.CommonFields;
import com.sunnada.nginx.exception.CustonException;
import com.sunnada.nginx.utils.PropsUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Configer {

    @Value("${nginx.path}")
    private String nginxPath;

    @Value("${nginx.config}")
    private String nginxConfPath;
    private static String NginxPath = null;

    private static String NginxConfPath = null;

    /**
     * 获得Nginx所在的路径
     *
     * @return : String
     * @Description :
     * @Creation Date : 2018年2月1日 下午5:11:45
     * @Author : 郭胜凯
     */
    public static String getNginxPatht() {
        if (null == NginxPath) {
            String string = PropsUtils.get(CommonFields.PATH.PATH_NGINX);
            if (null == string || "".equals(string.trim())) {
                string = System.getProperty("user.dir") + "/nginx";
            }
            NginxPath = string;
        }
        return NginxPath;
    }

    public String getNginxPath() throws CustonException {
        if (StringUtils.isBlank(nginxPath)) throw new CustonException("nginx根目录未设置");
        return nginxPath;
    }

    public String getNginxConfPath() throws CustonException {
        if (StringUtils.isBlank(nginxConfPath)) {
            StringBuilder stringBuilder = new StringBuilder(getNginxPath());
            stringBuilder.append("/conf/nginx.conf");
            return stringBuilder.toString();
        }
        return nginxConfPath;
    }

    /**
     * 获得Nginx的配置文件所在的路径
     *
     * @return : String
     * @Description :
     * @Creation Date : 2018年2月1日 下午5:11:33
     * @Author : 郭胜凯
     */
    public static String getNginxConfPatht() {
        if (null == NginxConfPath) {
            String string = PropsUtils.get(CommonFields.PATH.PATH_NGINX_CONF);
            if (null == string) {
                NginxConfPath = getNginxPatht() + "/conf/nginx.conf";
            } else {
                NginxConfPath = string;
            }
        }
        return NginxConfPath;
    }

}
